import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.management.Query;

public class MyTest {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public boolean isValidBST(TreeNode root) {
        return VLR(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }
    private boolean VLR(TreeNode root,long lower,long upper){
        if(root==null){
            return true;
        }
        if(root.val>=upper||root.val<=lower){
            return false;
        }
        return VLR(root.left, lower, root.val)&&VLR(root.right, root.val, upper);
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        if(root==null){
            return ans;
        }
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        queue.add(root);
        while(!queue.isEmpty()){
            ArrayList<Integer> arrayList=new ArrayList<Integer>();
            for(int i=0;i<queue.size();i++){
                TreeNode curr=queue.poll();
                arrayList.add(curr.val);
                if(curr.left!=null){
                    queue.add(curr.left);
                }
                if(curr.right!=null){
                    queue.add(curr.right);
                }
            }
            ans.add(arrayList);
        }
        return ans;
    }

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        
    }

}
